import 'package:flutter_module/common/base/page/BasicListPage.dart';
import 'package:flutter_module/model/book.dart';
import 'package:flutter_module/res/colors.dart';
import 'package:flutter/material.dart';
import 'package:flutter_boost/flutter_boost.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import '../../../common/base/page/BasePage.dart';
import '../../../common/utils/aliyunImage.dart';
import '../../../res/image.dart';
import 'book_search_logic.dart';
import 'book_search_state.dart';

class BookSearchPage extends BasePage {
  final BookSearchLogic logic = Get.put(BookSearchLogic());
  final BookSearchState state = Get.find<BookSearchLogic>().state;

  BookSearchPage({super.key});

  @override
  State<BookSearchPage> createState() => _BookSearchPageState();
}

class _BookSearchPageState
    extends BasicListPage<BookSearchPage, BookSearchLogic> {
  @override
  BookSearchLogic get currentLogic => widget.logic;

  @override
  Widget? emptyWidget() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [_emptyCell(), _addCell()],
    );
  }

  @override
  Widget? header() {
    return _header();
  }

  @override
  Widget listItem(int i) {
    return _cell(currentLogic.dataList[i]);
  }

  @override
  String screenName() {
    return "书籍搜索页";
  }

  Widget textContainer(Book book) {
    return Expanded(
      child: Container(
        padding: const EdgeInsets.only(left: 18, right: 10),
        child: Column(
          children: [
            SizedBox(
              height: 32,
            ),
            Text(
              '${book.bookTitle ?? ''}',
              overflow: TextOverflow.ellipsis,
              style: TextStyle(fontSize: 14, color: YouthColors.colorContent),
            ),
          ],
          crossAxisAlignment: CrossAxisAlignment.start,
        ),
      ),
    );
  }

  Container _header() {
    return Container(
      height: 44,
      child: Row(
        children: [
          SvgPicture.asset(
            YouthImages.icSearchOrange,
            width: 32,
            height: 32,
          ),
          const SizedBox(
            width: 5,
          ),
          Expanded(
              child: GetBuilder<BookSearchLogic>(
            init: BookSearchLogic(),
            builder: (_) {
              return Container(
                alignment: Alignment.centerLeft,
                child: TextField(
                  textAlign: TextAlign.start,
                  controller: currentLogic.state.tfController,
                  decoration: InputDecoration(
                    border: InputBorder.none,
                    suffixIcon: Visibility(
                      visible: currentLogic.state.isShowClearBtn,
                      child: FloatingActionButton(
                        onPressed: () => currentLogic.clearSearchText(),
                        child:
                            Image(image: AssetImage(YouthImages.icInputDelete)),
                        backgroundColor: Colors.white,
                        elevation: 0,
                      ),
                    ),
                  ),
                  focusNode: currentLogic.state.bookFocusNode,
                  onChanged: (value) => currentLogic.completeInput(value),
                  onSubmitted: (value) => currentLogic.completeInput(value),
                ),
              );
            },
          )),
          SizedBox(
            width: 10,
          ),
          FloatingActionButton(
            backgroundColor: Colors.white,
            elevation: 0,
            onPressed: () => BoostNavigator.instance.pop(),
            child: const Text('取消',
                style: TextStyle(fontSize: 17, color: Color(0xFF453838))),
          ),
          SizedBox(
            width: 5,
          ),
        ],
      ),
    );
  }

// 搜索空白
  Widget _emptyCell() {
    return Container(
      color: Colors.white,
      height: 67,
      child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
        Text(
          '书籍暂未收录，试试手动添加',
          overflow: TextOverflow.ellipsis,
          style: TextStyle(fontSize: 14, color: YouthColors.color_6A6A6A),
        )
      ]),
    );
  }

// 添加书籍
  Widget _addCell() {
    return Container(
      color: Colors.white,
      child: InkWell(
        onTap: () {
          hideKeyboard(context);
          currentLogic.pushAddBook();
        },
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const SizedBox(
              width: 20,
            ),
            Padding(
              padding: const EdgeInsets.only(top: 22),
              child: SvgPicture.asset(
                YouthImages.icBookAddSmall,
                width: 10,
                height: 10,
              ),
            ),
            const SizedBox(
              width: 12,
            ),
            Padding(
              padding: const EdgeInsets.only(top: 17),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Container(
                    width: 300,
                    child: Text(
                      currentLogic.state.searchText,
                      overflow: TextOverflow.ellipsis,
                      textAlign: TextAlign.left,
                      style: const TextStyle(
                          fontSize: 14,
                          color: YouthColors.color_333333,
                          fontWeight: FontWeight.w600),
                    ),
                  ),
                  const SizedBox(
                    height: 3,
                  ),
                  const Text(
                    '添加书籍',
                    overflow: TextOverflow.ellipsis,
                    textAlign: TextAlign.left,
                    style: TextStyle(
                        fontSize: 14, color: YouthColors.color_535353),
                  ),
                  const SizedBox(
                    height: 16,
                  )
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _cell(Book book) {
    return InkWell(
      child: Container(
        child: Row(
          children: [
            _bookCover(book),
            textContainer(book),
            _selectedBtn(book),
          ],
          crossAxisAlignment: CrossAxisAlignment.start,
        ),
        constraints: BoxConstraints(minHeight: 90),
        color: Colors.white,
      ),
    );
  }

  Widget _selectedBtn(Book book) {
    return Padding(
      padding: const EdgeInsets.only(right: 15),
      child: Column(
        children: [
          SizedBox(
            height: 30,
          ),
          Container(
            child: FloatingActionButton(
              child: Text('添加',
                  style: TextStyle(
                      fontSize: 14,
                      color: YouthColors.colorPrimary,
                      fontWeight: FontWeight.w500)),
              backgroundColor: Color(0xFFFFECE3),
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(15))),
              elevation: 0,
              onPressed: () => currentLogic.selectBtnClicked(book),
            ),
            height: 30,
            width: 70,
          ),
        ],
        mainAxisAlignment: MainAxisAlignment.start,
      ),
    );
  }

  Widget _bookCover(Book book) {
    return Padding(
      padding: const EdgeInsets.only(top: 10, left: 16),
      child: Column(
        children: [
          ClipRRect(
            child: FadeInImage(
              placeholder: AssetImage(YouthImages.icBitmapPureWhite),
              image: NetworkImage(YZAliyunImage.bmiddleImage(book.bookCover)),
              width: 40,
              height: 58,
              imageErrorBuilder: (context, error, stackTrace) {
                return Image.asset(
                  YouthImages.icDefaultBookCover,
                  width: 40,
                  height: 58,
                );
              },
              fit: BoxFit.cover,
            ),
            borderRadius: BorderRadius.circular(4),
          ),
        ],
        mainAxisAlignment: MainAxisAlignment.start,
      ),
    );
  }
}
